import Mock from 'mockjs'  

// let List = []
// const count = 200
// //模拟200条用户数据
// for (let i = 0; i < count; i++) {
//   List.push(
//     Mock.mock({
//       id: Mock.Random.guid(),
//       name: Mock.Random.cname(),
//       addr: Mock.mock('@county(true)'),
//       'age|18-60': 1,
//       birth: Mock.Random.date(),
//       sex: Mock.Random.integer(0, 1)
//     })
//   )
// }
  
const notice = {
  newsList:[]
}
const count = 5
//模拟200条用户数据
for (let i = 0; i < count; i++) {
  notice.newsList.push(
    Mock.mock({  
      id: "@increment",  
      content: "@csentence(50)",  
      publish_date: "@date(yyyy-MM-dd)",  
      'status|1': ['启用', '禁用'],  
      operator: "@cname()",  
      operate_time: "@date(yyyy-MM-dd hh:mm:ss)",  
    })
  )
}
export default {
  /**  
   * 获取通告列表  
   * @param status, searchKey, page, limit  
   * @return {{code: number, data: {pagenum: number, pagesize: number, content: Array, total: number, totalPages: number}}}  
   */  
  getNoticeList: config => {  
    const { status, searchKey, page = 1, limit = 10 } = JSON.parse(config.body);  
    let mockList = notice.newsList;  

    // // 过滤状态  
    // if (status === '1') {  // 启用  
    //   mockList = mockList.filter(user => user.status === '启用');  
    // } else if (status === '2') {  // 禁用  
    //   mockList = mockList.filter(user => user.status === '禁用');  
    // } else if (status === '3') {  // 全部  
    //   // mockList 保持所有通告，无需过滤  
    //   mockList = mockList
    // }  

    // 过滤搜索关键字  
    if (searchKey) {  
      mockList = mockList.filter(user => user.operator.includes(searchKey));  
    }  

    // 分页  
    const total = mockList.length;  
    const totalPages = Math.ceil(total / limit);  

    console.log(notice,'1111111')
    const paginatedData = mockList.slice((page - 1) * limit, page * limit);  
    console.log(paginatedData,'22222222')
    return {  
      code: 200,  
      data: {  
        pagenum: page,  
        pagesize: limit,  
        content: paginatedData,  
        total: total,  
        totalPages: totalPages,  
      }  
    };  
  }, 


  // 新增通告列表

  addNoticeList: config => {
      const body =  JSON.parse(config.body)
      notice.newsList.push(
        Mock.mock({
          id: "@increment",
          content: body.content,
          publish_date: body.publish_date,
          status: body.status,
          operator: "@cname()",
          operate_time: body.operate_time,
        })
      )
      console.log('  notice.newsList1111111', notice.newsList);
      return {
        code: 200,
        message: "添加成功",
        data: {
          content: notice.newsList,
          total: notice.newsList.length,
        }
      }
  },

  // 通知模块删除数据

  delNoticeList: config => {
      const body = JSON.parse(config.body)
      const index = notice.newsList.findIndex((item) => {
        return item.id === body.id
      });
      notice.newsList.splice(index, 1)
      return {
        code: 200,
        message: "删除成功",
        data: {
          content: notice,
          total: notice.newsList.length,
        }
      }

  },

  // 通知模块编辑数据

  editNoticeList: config => {
      const body = JSON.parse(config.body)
      var index = notice.newsList.findIndex((item) => {
        return item.id === body.id
      });
      notice.newsList.splice(index, 1, Mock.mock({
        id: "@increment",
        content: body.content,
        publish_date: body.publish_date,
        status: body.status,
        operator: body.operator,
        operate_time: body.operate_time,
      }))
      console.log(notice.newsList)
      return {
        code: 200,
        message: "编辑成功",
        data: {
          content: notice.newsList,
          total: notice.newsList.length,
        }
      }
    }
}
